<?php
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>TOUPROPRE DBMS</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" media="screen" type="text/css" title="Design" href="styles/design.css" />
</head>

<body>
    <!-- TITLE  -->
	<div id="page_title">
      <h1>View active clients/suppliers</h1>
    </div>

    <!-- Side Menues -->
    <?php include "tools/Menues.html"; ?>

    <!-- Main Body -->
    <div id="page_body">
<?php
/*
 * Includes
 */
include "tools/Queries.php";

/*
 * Function Definitions :
 */
function ShowSelectTable() {
    echo "<a href=active.php>Select Table</a>";
}
function ShowTableSelectForm() {
   echo "<p class='text'>This page will allow you to view all active clients/suppliers.</p>";
    echo "\n<p class='text'>Please select a table:</p>";
    echo "\n<form action='active.php' method='get'>";
    echo "\n<select name='table'>";
    echo "\n<option value='clients'>Clients</option>";
    echo "\n<option value='suppliers'>Suppliers</option>";
    echo "\n</select>";
    echo "\n<input type='submit' name='submit' value='Select'>";
    echo "\n</form>";
}
function ShowClientRow($clientID) {
    $sql = "SELECT * FROM clients WHERE clientID='$clientID'";
    $row = mysql_fetch_row(mysql_query($sql));
    $n = count($row);
    echo "\n<tr>";
    for($i=0; $i<$n; $i++) {
        echo "\n<td>".$row[$i]."</td>";
    }
    echo "\n</tr>";
}

/*
 * Behaviour:
 */
if(isset($_GET['table'])) {
    if($_GET['table']=='suppliers') {
        $sql = "SELECT * FROM suppliers WHERE BankRupt=0";

        include "tools/connect.php";
        $response = mysql_query($sql);
        if(!$response) {
            echo "<p class=sql_error>".mysql_error()."</p>";
        }
        else {
            EchoTable($response);
        }
        mysql_close();
    }
    else if($_GET['table']=='clients') {
        include "tools/connect.php";
        // Get Column Names
        $sql = "SELECT * FROM clients LIMIT 0,1";
        $response = mysql_query($sql);
        $i = 0;
        while($i < mysql_num_fields($response)) {
            $meta = mysql_fetch_field($response,$i);
            $column[$i] = $meta->name;
            $i++;
        }

        // Get all records that are < 1 year old :
        //                      HHMMSS             YMMDD  HHMMSS
        $lastyear = "CURDATE()*1000000+CURTIME() - 10000*1000000";
        $sql = "SELECT clientID FROM sales WHERE datetime>$lastyear ORDER BY clientID";
        $response = mysql_query($sql);
        if(!$response) {echo "<p class=sql_error>".mysql_error()."</p>";}
        //EchoTable($response);
    
        // Show first line of the table
        echo "\n\n<table frame=box>\n<tr bgcolor=#000077>";
        $n = count($column);
        for($i=0; $i<$n; $i++) {
            echo "\n<td><font color=#ffffff>".$column[$i]."</font></td>";
        }
        echo "\n</tr>";

        $id = '';
        $row = mysql_fetch_row($response);
        while($row != false) {
            if($row[0] != $id) {
                $id = $row[0];
                ShowClientRow($id);
            }
            else {/* Skip duplicate */}

            $row = mysql_fetch_row($response);
        }
        echo "\n</table>";

        mysql_close();
    }
    else {
        echo "<p class=text>Unknown table.</p>";
    }
}
else {
    ShowTableSelectForm();
}
?>
        
    </div>
    
    <!-- Footer -->
    <?php include "tools/Footer.html"; ?>
</body>
</html>
